package dashboard.net;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.List;

public class NetworkTest {

	public static void main(String[] args) throws UnknownHostException, SocketException {
		String ipAddress= getIp();
		System.out.println(InetAddress.getLocalHost().getHostAddress());
		System.out.println(InetAddress.getLocalHost().getCanonicalHostName());
		
		//给定一个主机名，返回主机名的地址
		InetAddress ia = InetAddress.getByName("www.baidu.com");
		System.out.println(ia.getHostName());
		System.out.println(ia.getHostAddress());
		System.err.println("-------------------------");
		
		//给定一个主机名，返回一个数组，包含所有对应此主机名的地址
		InetAddress[] ias = InetAddress.getAllByName("www.baidu.com");
		for (int i = 0; i < ias.length; i++) {
			System.out.println(ias[i]);
		}
		
		System.err.println("-------------------------");
		InetAddress iad = InetAddress.getByName("127.0.0.1");
		 if(!iad.getHostName().equalsIgnoreCase(iad.getHostAddress())) {
			System.out.println(iad.getHostName());
			System.out.println(iad.getHostAddress());
		}
		
		System.err.println("-------------------------");
		Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
		while (netInterfaces.hasMoreElements()) {
			NetworkInterface nif = netInterfaces.nextElement();
//			System.out.println(nif.getName()+" : "+nif.getDisplayName());
			Enumeration<InetAddress> iparray = nif.getInetAddresses();
			while (iparray.hasMoreElements()) {
				InetAddress ip = iparray.nextElement();
				if (ip instanceof Inet6Address) {
                    continue;
                }
				if(ip.isLoopbackAddress() || ip.isSiteLocalAddress()){
					System.out.println("LoopbackAddress or SiteLocalAddress: " +ip.getHostAddress());
				}
				System.out.println("\t" + ip.getHostAddress());
			}
		}
			
		System.err.println("-------------------------");
		InetAddress ins = InetAddress.getLocalHost();
//		InetAddress ins = InetAddress.getByName("www.baidu.com");
		System.err.println("**********ins**************");
		   System.out.println(ins);
		   NetworkInterface ni = NetworkInterface.getByInetAddress(ins);
		   System.err.println("**********NetworkInterface.getName()**************");
		   System.out.println(ni.getName());
		   System.err.println("**********NetworkInterface.getDisplayName()**************");
		   System.out.println(ni.getDisplayName());
		   //获取物理地址
		   System.err.println("**********NetworkInterface.getHardwareAddress()**************");
		   for(byte bs:ni.getHardwareAddress())
		   {
			   System.out.print(bs+".");
		   }
		   System.out.println("");
		   System.err.println("**********NetworkInterface.getInetAddresses**************");
		   Enumeration<InetAddress> inets = ni.getInetAddresses();
		   while(inets.hasMoreElements())
		   {
		    System.out.println(inets.nextElement());
		   }
		   System.err.println("**********NetworkInterface.getInterfaceAddresses**************");
		   List<InterfaceAddress> ilist = ni.getInterfaceAddresses();
		   for(InterfaceAddress ifadd :ilist)
		   {
		    System.out.println(ifadd);
		   }
	}
	
	
	/**
	 * 多IP处理，可以得到最终ip
	 * @return
	 */
	public static String getIp() {
		String localip = null;// 本地IP，如果没有配置外网IP则返回它
		String netip = null;// 外网IP
		try {
			Enumeration<NetworkInterface> netInterfaces = NetworkInterface
					.getNetworkInterfaces();
			InetAddress ip = null;
			boolean finded = false;// 是否找到外网IP
			while (netInterfaces.hasMoreElements() && !finded) {
				NetworkInterface ni = netInterfaces.nextElement();
				Enumeration<InetAddress> address = ni.getInetAddresses();
				while (address.hasMoreElements()) {
					ip = address.nextElement();
//					System.out.println(ni.getName() + ";" + ip.getHostAddress()
//							+ ";ip.isSiteLocalAddress()="
//							+ ip.isSiteLocalAddress()
//							+ ";ip.isLoopbackAddress()="
//							+ ip.isLoopbackAddress());
					if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
							&& ip.getHostAddress().indexOf(":") == -1) {// 外网IP
						netip = ip.getHostAddress();
						finded = true;
						break;
					} else if (ip.isSiteLocalAddress()
							&& !ip.isLoopbackAddress()
							&& ip.getHostAddress().indexOf(":") == -1) {// 内网IP
						localip = ip.getHostAddress();
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		if (netip != null && !"".equals(netip)) {
			return netip;
		} else {
			return localip;
		}
	}
}
